package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.ICityDao;
import com.magicode.cms.entiy.base.City;
import com.magicode.cms.service.ICityService;
@Service("serviceCity")
public class CityServiceImpl extends BaseServiceImpl<City,ICityDao> implements ICityService,UtilPath {

	public List<Map<String,Object>> select(Map<String,String> querywhere){
		try {
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("valid", ""+entity_alias_name+".valid as valid ");
			gridsel.put("provinceId", ""+entity_alias_name+".province.id as provinceId ");
			gridsel.put("provinceTitle", ""+entity_alias_name+".province.title as provinceTitle ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	/**
	 * 
	 */
	public List<Map> simpleSelect(Map<String,String> querywhere){
		try {
			List<Map> resultList = new ArrayList();
//			查询省份SQL
			String provinceHql = " select new map ( "
					+ " t.id as id , t.title as p "
					+ " ) "
					+ " from com.magicode.cms.entiy.base.Province t where t.valid = 1 "
					+ " order by t.orderCode asc ";
			List proList = this.queryDataList(provinceHql);
//			查询城市SQL
			String cityHql = " select new map ( "
					+ " t.id as id , t.title as n , p.id as pId "
					+ " ) "
					+ " from com.magicode.cms.entiy.base.City t "
					+ " left join t.province as p where t.valid = 1 order by t.orderCode asc  ";
			List cityList = this.queryDataList(cityHql);
			for(Object proO : proList){
				if(proO instanceof Map ){
					String pId = (String) ((Map)proO).get("id");
					List city1List = new ArrayList();
					for(Object cityO : cityList){
						if(cityO instanceof Map ){
							String cId = (String) ((Map)cityO).get("id");
							String cpId = (String) ((Map)cityO).get("pId");
//							如果相同
							if(cpId != null && pId != null && cpId.equals(pId)){
//								清除pId
								((Map) cityO).put("pId", null);
								city1List.add(cityO);
								continue;
							}
						}
					}
					((Map) proO).put("c", city1List);
				}
				resultList.add((Map<String,Object>)proO);
			}
			
			return resultList;
		} catch (Exception e) {
			return null;
			
		}
	}
	/**
	 * 得到根据线下活动选择的城市
	 * pId
	 * pTitle
	 * value:List<Map>
	 * 		cId
	 * 		cTitle
	 */
	public List<Map> getOfflineListSelect(){
		try {
			List<Map> resultList = new ArrayList();
//			查询省份SQL
			String provinceHql = " select new map ( "
					+ " p.id as pId , p.title as pTitle ,"
					+ " c.id as cId , c.title as cTitle  "
					+ " ) "
					+ " from "
					+ " com.magicode.cms.entiy.base.Province p , "
					+ " com.magicode.cms.entiy.base.City  c  "
					+ " where "
					+ " p.valid = 1 and c.valid = 1 and c.province = p and  c.id in "
					+ " ( select o.city.id from  com.magicode.cms.entiy.base.OfflineEvents o where o.status = 2 )  "
					+ " order by p.orderCode asc , c.orderCode asc ";
			List proList = this.queryDataList(provinceHql);
			
			for(Object cityPro : proList){
				if(cityPro instanceof Map ){
					String pId = (String) ((Map)cityPro).get("pId");
					String pTitle = (String) ((Map)cityPro).get("pTitle");
					String cId = (String) ((Map)cityPro).get("cId");
					String cTitle = (String) ((Map)cityPro).get("cTitle");
//					第一次读取设置
					if(resultList.isEmpty()){
						Map pMap = new HashMap();
						pMap.put("pId", pId);
						pMap.put("pTitle", pTitle);
						List tempList = new ArrayList();
						Map cMap = new HashMap();
						cMap.put("cId", cId);
						cMap.put("cTitle", cTitle);
						tempList.add(cMap);
						pMap.put("value", tempList);
						resultList.add(pMap);
					}
					else{
//						遍历所有已设置List
						boolean proFlag = false;
						for(Map pMap : resultList){
							String tempPid = (String) pMap.get("pId");
//							如果匹配到省份
							if(pId.equals(tempPid)){
								proFlag = true;
								List<Map> cList = (List<Map>) pMap.get("value");
								if(cList == null){
									cList = new ArrayList();
								}
								Map cMap = new HashMap();
								cMap.put("cId", cId);
								cMap.put("cTitle", cTitle);
								cList.add(cMap);
								pMap.put("value", cList);
								break;
							}
						}
//						如果没有找到创建新的
						if(proFlag == false){
							Map pMap = new HashMap();
							pMap.put("pId", pId);
							pMap.put("pTitle", pTitle);
							List tempList = new ArrayList();
							Map cMap = new HashMap();
							cMap.put("cId", cId);
							cMap.put("cTitle", cTitle);
							tempList.add(cMap);
							pMap.put("value", tempList);
							resultList.add(pMap);	
						}
					}					
				}
			}
			
			return resultList;
		} catch (Exception e) {
			return null;
			
		}
	}
	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "名称");
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "code");
		map.put(json_title, "编号");
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql = " from "+City.class.getName()+" t where 1 = 1";
		if(null!=querywhere && !querywhere.isEmpty()){
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id='"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey(field_title)){
				hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get(field_title)+"%'";
			}
			if(querywhere.containsKey("provinceId")){
				hql = hql+" and "+entity_alias_name+".province.id='"+querywhere.get("provinceId")+"'";
			}
//			设置城市范围
			if(querywhere.containsKey("inHql")){
				hql = hql+" and "+entity_alias_name+".id in ("+querywhere.get("inHql")+")";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {

		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			return this.queryMapDataList(hql, gridsel,start , size);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
		
	
	}

	@Override
	public List<Map<String, Object>> simpleSelectAre(Map<String, String> querywhere) {
		try {
			String hql = this.getHql(querywhere);     
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as text ");
			return this.queryMapDataList(hql, gridsel,0 , 0);
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public ICityDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return daocity;
	}
	@Autowired
	@Qualifier("daoCity")
	ICityDao daocity;
	public ICityDao getDaocity() {
		return daocity;
	}

	public void setDaocity(ICityDao daocity) {
		this.daocity = daocity;
	}
	
	/**
	 * 国家：
	 * id,title
	 * countryList
	 * 省份：
	 * 		id,title,cid
	 *		provinceList
	 * 城市：		id,title,pid
	 *     		cityList
	 */
	@Override
	public List<Map> getCountryProvinceCity(Map<String,String> querywhere) {
		try {
			List<Map> resultList = new ArrayList();
			
//			查询国家SQL
			String countryHql = " select new map ( "
					+ " t.id as id , t.title as title "
					+ " ) "
					+ " from com.magicode.cms.entiy.base.Country t where t.valid = 1 "
					+ " order by t.orderCode desc ";
			List countryList = this.queryDataList(countryHql);
			
//			查询省份SQL
			String provinceHql = " select new map ( "
					+ " t.id as id , t.title as title , c.id as cid"
					+ " ) "
					+ " from com.magicode.cms.entiy.base.Province t "
					+ " left join t.country as c where t.valid = 1 "
					+ " order by t.orderCode asc ";
			List provinceList = this.queryDataList(provinceHql);
			
//			查询城市SQL
			String cityHql = " select new map ( "
					+ " t.id as id , t.title as title , p.id as pid "
					+ " ) "
					+ " from com.magicode.cms.entiy.base.City t "
					+ " left join t.province as p where t.valid = 1 order by t.orderCode asc  ";
			List cityList = this.queryDataList(cityHql);
			
			for(Object country : countryList){
				if(country instanceof Map){
					String countryId = (String) ((Map) country).get("id");
					List province1List = new ArrayList();
					for (Object province : provinceList) {
						if(province instanceof Map){
							String provinceId = (String) ((Map) province).get("id");
							String provinceCountryId = (String) ((Map) province).get("cid");
							
							if(provinceCountryId != null && countryId != null && provinceCountryId.equals(countryId)){
								
								List city1List = new ArrayList();
								for(Object city : cityList){
									if(city instanceof Map ){
										String cityId = (String) ((Map)city).get("id");
										String cityProvinceId = (String) ((Map)city).get("pid");
//										如果相同
										if(cityProvinceId != null && provinceId != null && cityProvinceId.equals(provinceId)){
//											清除pId
											((Map) city).put("pid", null);
											city1List.add(city);
											continue;
										}
									}
								}
								((Map) province).put("c", city1List);
								((Map) province).put("cid", null);
								province1List.add(province);
								continue;
							}
						}
					}
					((Map) country).put("p", province1List);
				}
				resultList.add((Map<String,Object>)country);
			}
			
//			for(Object province : provinceList){
//				if(province instanceof Map ){
//					String provinceId = (String) ((Map)province).get("provid");
//					List city1List = new ArrayList();
//					for(Object city : cityList){
//						if(city instanceof Map ){
//							String cityId = (String) ((Map)city).get("cityid");
//							String cityProvinceId = (String) ((Map)city).get("pid");
////							如果相同
//							if(cityProvinceId != null && provinceId != null && cityProvinceId.equals(provinceId)){
////								清除pId
//								((Map) city).put("pid", null);
//								city1List.add(city);
//								continue;
//							}
//						}
//					}
//					((Map) province).put("c", city1List);
//				}
//				resultList.add((Map<String,Object>)province);
//			}
			
			return resultList;
		} catch (Exception e) {
			return null;
			
		}
	}
	
	
}
